//
//  KLNormalSpeedViewController.m
//  KunLun
//
//  Created by Mac on 2017/11/30.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLNormalSpeedViewController.h"
#import "KLGoodTableViewCell.h"
@interface KLNormalSpeedViewController ()
@property(nonatomic, strong) STSegementButton                     *segementButton;
@property(nonatomic, strong) STNoresultView                     *noResultView;
@property(nonatomic, strong) NSMutableArray                     *dataSouce;
@end

@implementation KLNormalSpeedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"匀速折扣区";
    [self configSubView];
    // Do any additional setup after loading the view.
}

- (void)configSubView{
    
//    __weak typeof(self) weakSelf =  self;
//    self.segementButton = [[STSegementButton alloc] initWithFrame:CGRectMake(0, 64, UIScreenWidth, 44) andTitle:@[@"商品",@"店铺"] handle:^(STSegementButton *sender) {
//        //置空数据源，并且刷新，之后mj开始
//        [weakSelf.tableView reloadData];
//        [self.tableView st_showAnimationWithType:STAnimationTypekCATransitionFade duration:0.25];
//    }];
//    self.segementButton.backgroundColor = [UIColor whiteColor];
//    self.segementButton.lineSelectedColor = KL_BlueBackGroundColor;
//    self.segementButton.butTitleSelectedColor = KL_BlueBackGroundColor;
//    self.segementButton.butTitleColor = KL_secendTextColor;
//    self.segementButton.lineColor = [UIColor whiteColor];
//    self.segementButton.cureentIndex = 0;
//    self.segementButton.autoMoveWithClic = YES;
//    [self.view addSubview:self.segementButton];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.noResultView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 180, UIScreenWidth, 300)
                                                        title:@"抱歉,暂无折扣券商品"
                                                  buttonTitle:@""
                                                 buttonHandle:^(NSString *titleString) {
                                                     
                                                 }];
    self.noResultView.hidden = YES;
    [self.tableView addSubview:self.noResultView];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendGoodListRequest];
    }];
    [self.tableView.mj_header beginRefreshing];

}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [KLGoodTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 44)];
    STButton * alertButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth , 44)
                                                       title:@"提示:匀加速折扣券使用方式"
                                                titleColor:KL_firstTextColor
                                                 titleFont:15
                                              cornerRadius:0
                                           backgroundColor:[UIColor whiteColor]
                                           backgroundImage:nil
                                                     image:[UIImage imageNamed:@"icon_-提示"]];
    alertButton.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    alertButton.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
    [alertButton setAttributedTitle:[alertButton.currentTitle st_convertAttributeStringWithKeyWord:@"提示:" attributes:@{NSForegroundColorAttributeName:KL_redColor}] forState:UIControlStateNormal];
    [sectionHeader addSubview:alertButton];
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    KLGoodTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[KLGoodTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.goodModel = self.dataSouce[indexPath.row];
    cell.moneyLable.text = @"折扣券";
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    KLGoodModel * model = self.dataSouce[indexPath.row];
    KLGoodDetailViewController * vc = [KLGoodDetailViewController new];
    vc.goodID = model.goodID.description;
    [self.navigationController pushViewController:vc animated:YES];
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (void)sendGoodListRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",serviceUrl,@"/api/goods/goodslist"];
//    NSString * num = [[KLClassify defult] numFormTitle:self.title];
    [dic setObject:@"3" forKey:@"classification"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 self.dataSouce = [NSMutableArray new];
                                                 NSDictionary * dataDic =  responseObject[@"data"];
                                                 if ([dataDic.allKeys containsObject:@"list"]) {
                                                     NSArray * list = dataDic[@"list"];
                                                     for (NSDictionary * listdic in list) {
                                                         KLGoodModel * model = [KLGoodModel  mj_objectWithKeyValues:listdic];
                                                         [self.dataSouce addObject:model];
                                                     }
                                                 }
                                                 self.noResultView.hidden = self.dataSouce.count;
                                                 [self.tableView reloadData];
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 self.noResultView.hidden = self.dataSouce.count;
                                                 [self.tableView.mj_header endRefreshing];
                                                 
                                             }];
}
@end
